# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=tigervnc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.14.1
pkgrel=2
pkgdesc="High performance, multi-platform VNC client and server (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://tigervnc.org'
msys2_repository_url="https://github.com/TigerVNC/tigervnc"
msys2_references=(
  'archlinux: tigervnc'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-nettle"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-libiconv"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools")
source=("https://github.com/TigerVNC/tigervnc/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-build-with-mingw-clang.patch"
        "0002-fix-fullscreen-api-conflicts.patch")
sha256sums=('579d0d04eb5b806d240e99a3c756b38936859e6f7db2f4af0d5656cc9a989d7c'
            'c5fd1d26bfb7461e3f61fe233ac648fc4f7421319a604f5bf59d3c9900184579'
            'fc112c1449ead798733c55c06b940b078f1cec5c2068872c5a5046fbde22e633')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-fix-build-with-mingw-clang.patch
  patch -p1 -i "${srcdir}"/0002-fix-fullscreen-api-conflicts.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_VIEWER=ON \
      -DENABLE_GNUTLS=ON \
      -DENABLE_H264=ON \
      -DENABLE_NETTLE=ON \
      -DENABLE_NLS=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENCE.TXT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
